<?php

namespace view;

class AdminView{

	private $datePosts ="datePosts";
	private $title = "title";
	private $posts = "posts";
	const sendPosts = "sendPosts";
	const deletePost = "deletePost";
	const showUpdatePost = "tryUpdatePost";
	const postID = "postID";
	const dateUpdate = "postUpdate";
	const titleUpdate = "titleUpdate";
	const postUpdate = "postUpdate";
	const updatePost = "updatePost";
	const commentPost = "commentPost";
		
	public function getAdminContent($posts){
		return $this->getAdminForm()
		.$this->getAdminBlogg($posts);
	}	
		
	public function getAdminForm(){
		return  "<div id='form'>
						<form action='?" . NavigationView::Controller . "=" . NavigationView::AdminController ."' method='post'> 
							<fieldset>
								<div id='contentAdmin'>
									<input type='text' name='$this->datePosts' value='datum'/></br>
						            <input type='text' name='$this->title' value='rubrik'/></br>				            
						            <textarea name='$this->posts' rows='20' cols='50'></textarea>
						           	<input type='submit' value='Skicka' name='".self::sendPosts."'/>
						           	<p>OBS! För att se om inlägget lagts till måste sidan uppdateras F5</p>
						       </div>
							</fieldset>
						</form>
					</div>"; 
	}
	
	public function getAdminBlogg($posts){
		$returnString = "";
		foreach($posts as $postsObject){
		$returnString .= "<form action='' method='post'> 
										<fieldset>
											<div id='contentHome'>
										        <article id='articleContentHome'>
										         	<p id='date'>$postsObject->datePosts</p>
										            <p id='title'>$postsObject->title</p>
													 <p id='post'>$postsObject->posts</p>
													 <input type='submit' value='Kommentera inlägget här...' name='".self::commentPost."'/>
													 <input type='submit' value='Ta bort' name='".self::deletePost."'/>
													<input type='submit' value='Uppdatera' name='".self::showUpdatePost."'/>
													<input type='text' name='" . self::postID . "' value='$postsObject->postsID' />
									            </article>
										   </div>
			   							</fieldset>
									</form>";
		}
		return $returnString;
	}

	public function updatePost($posts){
		foreach($posts as $postsObject){
			return "<form action='' method='post'> 
							<fieldset>
								<div>
							        <article>
							        	<input type='hidden' name='".self::postID."' value='$postsObject->postsID'/></br>
							            <input type='text' name='".self::dateUpdate."' value='$postsObject->datePosts'/></br>
					           			<input type='text' name='".self::titleUpdate."' value='$postsObject->title'/></br>	
					           			<textarea name='".self::postUpdate."' rows='20' cols='50' >$postsObject->posts</textarea>
							           	<section class='kommentar'>
										</section>
							             <input type='submit' value='uppdatera' class='button' name='".self::updatePost."'/> 
							        </article>
							    </div>
							</fieldset>
						</form>"; 
			}
		}
		
	public function createTextUpdate(){
		$posts = new \common\Posts();
			$obj = $posts->Create($_POST[self::postID], $_POST[self::postUpdate], $_POST[self::dateUpdate], $_POST[self::titleUpdate]);
		return $obj;
	}	
	
	/*----------------------------Fält och kommandon-----------------------------*/
	public function getPosts(){
		if(isset ($_POST[$this->posts])) {
			return $_POST[$this->posts];
		}
		return FALSE;
	}
	
	public function getDate(){
		if(isset ($_POST[$this->datePosts])) {
			return $_POST[$this->datePosts];
		}
		return FALSE;
	}
	
	public function getTitle(){
		if(isset ($_POST[$this->title])) {
			return $_POST[$this->title];
		}
		return FALSE;
	}
	
	public function triedToSendPosts(){
		if(isset ($_POST[self::sendPosts]) == TRUE){
			return TRUE;
		}
		return FALSE;
	}
	
	public function triedToDeletePost(){
		if(isset ($_POST[self::deletePost]) == TRUE){
			return TRUE;
		}
		return FALSE;
	}
	
	public function showUpdatePost(){
		if(isset ($_POST[self::showUpdatePost]) == TRUE){
			return TRUE;
		}
		return FALSE;
	}
	
	public function triedToUpdatePost(){
		if(isset ($_POST[self::updatePost]) == TRUE){
			return TRUE;
		}
		return FALSE;
	}
	
	public function triedToCommentPost(){
		if(isset ($_POST[self::commentPost]) == TRUE){
			return TRUE;
		}
		return FALSE;
	}
}